package cn.fl.fgi.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.fgi.api.condition.FgiPostResQueryCondition;
import cn.fl.fgi.service.IFgiPostResService;
import cn.fl.fgi.vo.FgiPostResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fgi/fgiPostRes")
public class FgiPostResController {

    @Autowired
    private IFgiPostResService fgiPostResService ;

    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("fgi/fgipostres/fgiPostResListPage");
        return view ;
    }

    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        String asynCode = request.getParameter("asynCode");

        if(StringUtils.isEmpty(page)){
            page = "1";
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize = "10";
        }
        FgiPostResQueryCondition condition = new FgiPostResQueryCondition();
        condition.setSize(Long.valueOf(pageSize));
        condition.setCurrent(Long.valueOf(page));
        if(StringUtils.isNotEmpty(asynCode)){
            condition.setAsynCode(asynCode);
        }
        Pager pager = this.fgiPostResService.searchListPage(condition);
        Map <String,String> map = this.getInfTypeMap();
        Map<String,String> fgiResCodeMap = this.getFgiResCodeMap();
        List<FgiPostResVO> voList = (List<FgiPostResVO>)pager.getResultList();
        if(CollectionUtils.isNotEmpty(voList)){
            for (FgiPostResVO vo: voList) {
                vo.setInfTypeNm(map.get(vo.getInfType()));
                vo.setAsynCodeNm(fgiResCodeMap.get(vo.getAsynCode()));
            }
            ListPageVO listPageVO = new ListPageVO<FgiPostResVO>(voList,pager.getTotalCount());

            return listPageVO ;
        }else{
            return new ListPageVO<FgiPostResVO>(new ArrayList<>(),0);
        }
    }

    private Map<String,String> getFgiResCodeMap(){
        Map<String,String> map = new HashMap<>();
        map.put("success","入库成功");
        map.put("hold","等待处理");
        map.put("failed","入库失败");
        map.put("isNot","批次号无效");
        return map ;
    }

    private Map<String,String> getInfTypeMap(){
        Map<String,String> map = new HashMap<>();
        map.put("1","用户数据接口");
        map.put("2","业务编号获取接口");
        map.put("3","业务信息接口");
        map.put("4","业务状态信息接口");
        map.put("5","交易流水信息接口");
        map.put("6","批次异步消息接口");
        map.put("7","每天推送批次数接口");
        map.put("8","推送批次列表接口");
        return map ;
    }
}
